package com.shidaimanong.demo.network;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class HttpClientDemo {

    /**
     * Http get请求
     */
    public static String doGet(String httpUrl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuilder result = new StringBuilder();
        try {
            //创建连接
            URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方式
            connection.setRequestMethod("GET");
            //开始连接
            connection.connect();
            //判断http返回码，获取响应数据
            if (connection.getResponseCode() == 200) {
                //获取返回的数据
                is = connection.getInputStream();
                if (null != is) {
                    //读取数据，其中GBK为指定字符集，这个需要根据接口返回的具体字符集来设置
                    //中文常用的字符集为UTF-8和GBK
                    br = new BufferedReader(new InputStreamReader(is, "GBK"));
                    String temp;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeConnection(connection, null, is, br);
        }
        return result.toString();
    }

    /**
     * Http post请求
     */
    public static String doPost(String httpUrl, String param) {
        StringBuilder result = new StringBuilder();
        HttpURLConnection connection = null;
        OutputStream os = null;
        InputStream is = null;
        BufferedReader br = null;
        try {
            //创建连接对象
            URL url = new URL(httpUrl);
            //创建连接
            connection = (HttpURLConnection) url.openConnection();
            //设置请求方法
            connection.setRequestMethod("POST");
            //DoOutput设置是否向httpUrlConnection输出，DoInput设置是否从httpUrlConnection读入
            connection.setDoOutput(true);
            connection.setDoInput(true);

            //拼装参数
            if (null != param && param.equals("")) {
                //设置参数
                os = connection.getOutputStream();
                //拼装参数
                os.write(param.getBytes(StandardCharsets.UTF_8));
            }

            //判断http返回码，并获取返回数据
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                if (null != is) {
                    //读取数据，其中GBK为指定字符集，这个需要根据接口返回的具体字符集来设置
                    //中文常用的字符集为UTF-8和GBK
                    br = new BufferedReader(new InputStreamReader(is, "GBK"));
                    String temp;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                        result.append("\r\n");
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConnection(connection, os, is, br);
        }
        return result.toString();
    }

    /**
     * 关闭连接
     */
    private static void closeConnection(HttpURLConnection connection, OutputStream os, InputStream is, BufferedReader br) {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        connection.disconnect();
    }

    public static void main(String[] args) {
        String message = doPost("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13000000000", "");
//        String message = doGet("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071");
        System.out.println(message);
    }
}